﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory

    Public Class CharacterStatisticsCategory
        Inherits WowArmoryData

#Region "Declarations"

        Private cscCategory As New CharacterStatisticsCategorySchema.category
        Private strRealm As String
        Private strName As String
        Private intCategoryID As Integer
        Private cCategory As New Category

#End Region

        ''' <summary>
        ''' The realm the character is on
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Realm() As String
            Get
                Return strRealm
            End Get
            Set(ByVal value As String)
                strRealm = value
            End Set
        End Property

        ''' <summary>
        ''' The name of the character
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Name() As String
            Get
                Return strName
            End Get
            Set(ByVal value As String)
                strName = value
            End Set
        End Property

        ''' <summary>
        ''' The category ID of the statistics to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property CategoryID() As Integer
            Get
                Return intCategoryID
            End Get
            Set(ByVal value As Integer)
                intCategoryID = value
            End Set
        End Property

        ''' <summary>
        ''' The URL that will be used to retrieve the character's statistics from the Armory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL() As String
            Get
                Return String.Format("http://www.wowarmory.com/character-statistics.xml?r={0}&n={1}&c={2}", strRealm, strName, intCategoryID)
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin() As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' The statistics for this category
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Statistics() As Category
            Get
                Return cCategory
            End Get
        End Property

        ''' <summary>
        ''' A constructor to load a character's statistics by realm, character name, and category ID
        ''' </summary>
        ''' <param name="strRealm"></param>
        ''' <param name="strName"></param>
        ''' <param name="intCategoryID"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strRealm As String, ByVal strName As String, ByVal intCategoryID As Integer)
            Me.strRealm = strRealm
            Me.strName = strName
            Me.intCategoryID = intCategoryID
            Load()
        End Sub

        ''' <summary>
        ''' Loads the character's statistics from the armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            Dim strKey As String = String.Format("LibWowArmory.CharacterStatisticsCategory.{0}.{1}.{2}", strRealm, strName, intCategoryID)
            If Cache.Cache(strKey) Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(cscCategory.GetType())

                Dim srXML As New StringReader(MyBase.strData)

                cscCategory = CType(xsSerializer.Deserialize(srXML), CharacterStatisticsCategorySchema.category)

                Cache.Cache.Insert(strKey, cscCategory, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.Normal, Nothing)
            Else
                cscCategory = CType(Cache.Cache(strKey), CharacterStatisticsCategorySchema.category)
            End If

            If cscCategory Is Nothing Then
                Throw New ArgumentException("The character you are trying to retrieve statistics for does not exist.")
            End If
            If cscCategory.category Is Nothing Then
                Throw New ArgumentException("The character you are trying to retrieve statistics for does not exist.")
            End If

            ProcessCategory(cCategory, cscCategory)
        End Sub

        Private Sub ProcessCategory(ByRef cCategory As Category, ByRef cscCategory As CharacterStatisticsCategorySchema.category)
            For Each cscStatistic As CharacterStatisticsCategorySchema.statistic In cscCategory.statistic
                cCategory.Statistics.Add(New Statistic(cscStatistic.id, cscStatistic.highest, cscStatistic.name, cscStatistic.quantity))
            Next
            For Each cscSubcategory As CharacterStatisticsCategorySchema.category In cscCategory.category
                Dim cSubcategory As New Category
                ProcessCategory(cSubcategory, cscSubcategory)
                cCategory.Subcategories.Add(cSubcategory)
            Next
        End Sub

        ''' <summary>
        ''' A class that encapsulates statistic category information
        ''' </summary>
        ''' <remarks></remarks>
        Public Class Category

            Private sStatistics As New List(Of Statistic)
            Private cSubcategories As New List(Of Category)

            ''' <summary>
            ''' The statistics in this category
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Statistics() As List(Of Statistic)
                Get
                    Return sStatistics
                End Get
                Set(ByVal value As List(Of Statistic))
                    sStatistics = value
                End Set
            End Property

            ''' <summary>
            ''' The subcategories listed in this category
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Subcategories() As List(Of Category)
                Get
                    Return cSubcategories
                End Get
                Set(ByVal value As List(Of Category))
                    cSubcategories = value
                End Set
            End Property

        End Class

    End Class

End Namespace